﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Space_Conquest
{

    delegate void TextBoxEnterHandler();

    class TextBox : IRender, IInput
    {

        #region Private Members

        protected static Texture _textBoxTexutre = new Texture("Images/TextBox.bmp");
        protected static Sprite _textBoxSprite = new Sprite(0, 0, 426, 32, 255, false);

        protected string _text;
        protected bool _isSelected;

        protected int _x;
        protected int _y;
        protected int _width;
        protected int _height;

        #endregion

        #region Properties

        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
            }
        }

        public int X
        {
            get
            {
                return _x;
            }
        }

        public int Y
        {
            get
            {
                return _y;
            }
        }

        public int Width
        {
            get
            {
                return _width;
            }
        }

        public int Height
        {
            get
            {
                return _height;
            }
        }

        public event TextBoxEnterHandler OnEnterPressed;

        #endregion

        #region Constructors

        public TextBox(int x, int y)
        {

            _x = x;
            _y = y;

            _width = _textBoxTexutre.Width;
            _height = _textBoxTexutre.Height;

            _text = "";
            _isSelected = false;

        }

        #endregion

        #region Methods

        #endregion

        #region IRender Members

        public virtual void Render()
        {

            _textBoxSprite.Left = _x;
            _textBoxSprite.Top = _y;

            _textBoxSprite.Render(_textBoxTexutre);
            Space_Conquest.Text.Render(_text, Color.White, _x + 10, _y + 5);


        }

        #endregion

        #region IInput Members

        public virtual bool MouseDown(System.Windows.Forms.MouseButtons button, int x, int y)
        {
            if (x >= _x && y >= _y && x <= _x + _width && y <= _y + _height)
            {
                _isSelected = true;
            }
            else
            {
                _isSelected = false;
            }
            return false;
        }

        public virtual bool MouseUp(System.Windows.Forms.MouseButtons button, int x, int y)
        {
            return false;
        }

        public virtual bool MouseMove(int x, int y)
        {
            return false;
        }

        public virtual bool KeyDown(System.Windows.Forms.Keys key, bool shift)
        {
            if (_isSelected)
            {
                if (key == Keys.Return)
                {
                    if (OnEnterPressed != null)
                        OnEnterPressed();
                }
                else
                {
                    _text = Helpers.ParseKey(_text, key, shift);
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public virtual bool KeyUp(System.Windows.Forms.Keys key, bool shift)
        {
            return false;
        }

        #endregion
    }
}
